//
//  RadioButton.m
//  RadioButton
//
//  Created by cuibaoyin on 14-11-17.
//  Copyright (c) 2014年 cby. All rights reserved.
//

#import "RadioButton.h"
#import "RadioButtonCell.h"

@interface RadioButton()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *myTableView;
    
    NSMutableArray *titleArray;
    
    CGFloat cellSumHeight;
}

@end

@implementation RadioButton

- (instancetype)init
{
    NSAssert(NO, @"You should never call this method in this class. Use (initWithFrame: titleArray:) instead!");
    return nil;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    NSAssert(NO, @"You should never call this method in this class. Use (initWithFrame: titleArray:) instead!");
    return nil;
}

- (instancetype)initWithFrame:(CGRect) frame titleArray:(NSArray *)array
{
    if (self = [super initWithFrame:frame])
    {
        //设置默认值
        _numberOfRadio = [array count];
        _normalRadioImage = [UIImage imageNamed:@"RadioButton-Unselected.png"];
        _selectedRadioImage = [UIImage imageNamed:@"RadioButton-Selected.png"];
        _selectedRadioIndex = 0;
        
        //label字体属性
        _titleFont = [UIFont boldSystemFontOfSize:15];
        _titleAttribute = [NSMutableDictionary dictionaryWithCapacity:0];
        _titleAttribute[NSFontAttributeName] = _titleFont;
        _titleColor = [UIColor blackColor];
        _titleAttribute[NSForegroundColorAttributeName] = _titleColor;
        
        titleArray = [NSMutableArray arrayWithArray:array];
        cellSumHeight = 0;

        //初始化subviews
        myTableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
        myTableView.dataSource = self;
        myTableView.delegate = self;
        myTableView.scrollEnabled = NO;
        myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self addSubview:myTableView];
    }
    return self;
}

- (void)layoutSubviews
{
    CGRect frame = self.frame;
    frame.size.height = cellSumHeight;
    self.frame = frame;
    
    frame.origin = CGPointZero;
    myTableView.frame = frame;
    
    if ([_delegate respondsToSelector:@selector(radioButton:heightChanged:)])
    {
        [_delegate radioButton:self heightChanged:cellSumHeight];
    }
    
    [super layoutSubviews];
}

#pragma mark - Setter && Getter
- (void)setSelectedRadioIndex:(NSInteger)selectedRadioIndex
{
    _selectedRadioIndex = selectedRadioIndex;
    [myTableView reloadData];
}

- (void)setNormalRadioImage:(UIImage *)normalRadioImage
{
    _normalRadioImage = normalRadioImage;
    [myTableView reloadData];
}

- (void)setSelectedRadioImage:(UIImage *)selectedRadioImage
{
    _selectedRadioImage = selectedRadioImage;
    [myTableView reloadData];
}

- (void)setTitleFont:(UIFont *)titleFont
{
    _titleFont = titleFont;
    _titleAttribute[NSFontAttributeName] = _titleFont;
    
    [myTableView reloadData];
}

- (void)setTitleColor:(UIColor *)titleColor
{
    _titleColor = titleColor;
    _titleAttribute[NSForegroundColorAttributeName] = _titleColor;
    [myTableView reloadData];
}

- (void)setTitleAttribute:(NSDictionary *)titleAttribute
{
    _titleAttribute = [NSMutableDictionary dictionaryWithDictionary:titleAttribute];
    [myTableView reloadData];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return titleArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"RadioButtonCell";
    RadioButtonCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell)
    {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"RadioButtonCell" owner:self options:nil] lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    NSAttributedString *titleAttributeString = [[NSAttributedString alloc] initWithString:titleArray[indexPath.row] attributes:_titleAttribute];
    cell.radioCell_label.attributedText = titleAttributeString;
    cell.radioCell_imageview.image = (indexPath.row == _selectedRadioIndex ? _selectedRadioImage : _normalRadioImage);
    [cell setNeedsLayout];
    
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat width = self.frame.size.width - RadioCellImageViewLeftCap - RadioCellImageViewHeight - RadioCellImageViewAndTitleLabelCap;
    NSAttributedString *titleAttributeString = [[NSAttributedString alloc] initWithString:titleArray[indexPath.row] attributes:_titleAttribute];
    CGFloat cellHeight = [RadioButtonCell heightForCellWithConstraintWidth:width attibuteString:titleAttributeString];
    
    cellSumHeight += cellHeight;
    return cellHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    _selectedRadioIndex = indexPath.row;
    
    [tableView reloadData];
    
    if ([_delegate respondsToSelector:@selector(radioButton:didSelectItemAtIndexPath:)])
    {
        [_delegate radioButton:self didSelectItemAtIndexPath:indexPath.row];
    }
}

@end
